package com.javasee.common.dbsharding.router;


import com.javasee.common.dbsharding.DbShardingRouter;

public class MurmurHashRouter implements DbShardingRouter {

    private Integer tbSize;

    private Integer dbSize;

    public MurmurHashRouter(Integer dbSize, Integer tbSize) {
        this.tbSize = tbSize;
        this.dbSize = dbSize;
    }

    @Override
    public Integer getTbIndex(String shardingParam) {
        return (int) (Math.abs(MurmurHash.hash(shardingParam)) % tbSize);
    }

    @Override
    public Integer getDbIndex(String shardingParam) {
        return (int) (Math.abs(MurmurHash.hash(shardingParam)) % tbSize / (tbSize / dbSize));
    }

    @Override
    public String getRealTbName(String tbName, Integer tbIndex) {
        return tbName + "_" + (tbIndex + 1);
    }

    public static void main(String[] args) {
        long r = Math.abs(MurmurHash.hash("11824104") % 256);
        System.out.println(r);
    }
}
